package com.kk.producer.comfig;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class RabbitmqConfig {
    // ack
    public static final String CONFIRM_EXCHANGE_NAME = "confirm_exchange";
    public static final String CONFIRM_QUEUE_NAME = "confirm_queue";
    public static final String ROUTING_KEY = "confirm";

    // qos
    public static final String CONFIRM_EXCHANGE_NAME2 = "qos_exchange";
    public static final String CONFIRM_QUEUE_NAME2 = "qos_queue";
    public static final String ROUTING_KEY2 = "qos";

    // ttl
    public static final String TEST_EXCHANGE_TTL = "test_exchange_ttl";
    public static final String CONFIRM_QUEUE_NAME1 = "queue_ttl";
    public static final String ROUTING_KEY1 = "ttl";

    /*声明交换机*/
    @Bean
    public DirectExchange confirmExchange(){
        return new DirectExchange(CONFIRM_EXCHANGE_NAME);
    }

    /*声明队列*/
    @Bean
    public Queue confirmQueue(){
        return QueueBuilder.durable(CONFIRM_QUEUE_NAME).build();
    }

    /*绑定交换机和队列*/
    @Bean
    public Binding exchangeBindingQueue(@Qualifier("confirmExchange") DirectExchange confirmExchange,
                                        @Qualifier("confirmQueue") Queue confirmQueue){
        return BindingBuilder.bind(confirmQueue).to(confirmExchange).with(ROUTING_KEY);
    }
    /*声明交换机*/
    @Bean
    public DirectExchange confirmExchange2(){
        return new DirectExchange(CONFIRM_EXCHANGE_NAME2);
    }

    /*声明队列*/
    @Bean
    public Queue confirmQueue2(){
        return QueueBuilder.durable(CONFIRM_QUEUE_NAME2).build();
    }

    /*绑定交换机和队列*/
    @Bean
    public Binding exchangeBindingQueue2(@Qualifier("confirmExchange2") DirectExchange confirmExchange,
                                        @Qualifier("confirmQueue2") Queue confirmQueue){
        return BindingBuilder.bind(confirmQueue).to(confirmExchange).with(ROUTING_KEY2);
    }
    /*声明交换机*/
    @Bean
    public DirectExchange confirmExchange1(){
        return new DirectExchange(TEST_EXCHANGE_TTL);
    }

    /*声明队列*/
    @Bean

    public Queue confirmQueue1(){
        final HashMap<String, Object> arguments
                = new HashMap<>();

        //设置TTL设置1秒过期
        arguments.put("x-message-ttl",1000);
        return QueueBuilder.durable(CONFIRM_QUEUE_NAME1).withArguments (arguments).build();
    }

    /*绑定交换机和队列*/
    @Bean
    public Binding exchangeBindingQueue1(@Qualifier("confirmExchange1") DirectExchange confirmExchange,
                                        @Qualifier("confirmQueue1") Queue confirmQueue){
        return BindingBuilder.bind(confirmQueue).to(confirmExchange).with(ROUTING_KEY1);
    }
}
